package com.dss.auto_deploy.controller;

import com.dss.auto_deploy.dto.AdmDbAddDto;
import com.dss.auto_deploy.dto.AdmDbPageDto;
import com.dss.auto_deploy.dto.AdmDbUpdateDto;
import com.dss.auto_deploy.service.IAdmDbService;
import com.dss.auto_deploy.util.Result;
import com.dss.auto_deploy.vo.AdmDbVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import java.util.List;

/**
 * 数据库
 *
 * @author dongshoushan
 * @Description:数据库控制层
 * @date 2022-10-29 13:00:39
 */
@RestController
@RequestMapping("/admDb")
@RequiredArgsConstructor(onConstructor = @_(@Autowired))
@Api(tags = "99: 数据库")
public class AdmDbController {

    /**
     * 数据库Service
     */
    private final IAdmDbService admDbService;
    private final HttpServletRequest request;

    /**
     * 新增数据库
     *
     * @param addDto
     * @return
     */
    @PostMapping
    @ApiOperation(value = "新增数据库", notes = "新增数据库")
    public Result insert(@RequestBody @Valid AdmDbAddDto addDto) {
        admDbService.insert(addDto);
        return Result.success("新增成功");
    }

    /**
     * 修改数据库
     *
     * @param updateDto
     * @return
     */
    @PutMapping
    @ApiOperation(value = "修改数据库", notes = "修改数据库")
    public Result update(@RequestBody @Valid AdmDbUpdateDto updateDto) throws Exception {
        admDbService.update(updateDto);
        return Result.success("修改成功");
    }

    @GetMapping("/getByType")
    @ApiOperation(value = "查询数据库", notes = "查询数据库")
    public Result<List<AdmDbVo>> getByType(String type, String name) {
        List<AdmDbVo> voList = admDbService.getByType(type,name);
        return Result.success(voList);
    }

    /**
     * 批量删除数据库
     *
     * @param ids
     * @return
     */
    @DeleteMapping("/{ids}")
    @ApiOperation(value = "删除数据库", notes = "删除数据库")
    public Result deleteBatch(@PathVariable Long[] ids) {
        admDbService.deleteBatch(ids);
        return Result.success("删除成功");
    }

    /**
     * 分页查询数据库
     *
     * @param pageDto
     * @return
     */
    @GetMapping("/page")
    @ApiOperation(value = "分页查询数据库", notes = "分页查询数据库")
    public Result page(AdmDbPageDto pageDto) throws InterruptedException {
        return Result.success(admDbService.list(pageDto));
    }


    @ApiOperation(value = "mysql安装", notes = "mysql安装")
    @PostMapping(value = "installMysql", consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
    public Result installMysql(
            @RequestParam(value = "ids") String ids,
            @RequestParam(value = "tempPath") String tempPath,
            @RequestPart(value = "mysqlFile") MultipartFile mysqlFile) throws Exception {
        request.getInputStream().skip(request.getContentLength());
        admDbService.installMysql(ids, tempPath, mysqlFile);
        return Result.success();
    }

    @ApiOperation(value = "postgres安装", notes = "postgres安装")
    @PostMapping(value = "installPostgres", consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
    public Result installPostgres(
            @RequestParam(value = "ids") String ids,
            @RequestParam(value = "tempPath") String tempPath,
            @RequestPart(value = "postgresFile") MultipartFile postgresFile) throws Exception {
        request.getInputStream().skip(request.getContentLength());
        admDbService.installPostgres(ids, tempPath, postgresFile);
        return Result.success();
    }


    @ApiOperation(value = "mongo安装", notes = "mongo安装")
    @PostMapping(value = "installMongo", consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
    public Result installMongo(
            @RequestParam(value = "ids") String ids,
            @RequestParam(value = "clientMountPath") String clientMountPath,
            @RequestPart(value = "mongoFile") MultipartFile mongoFile,
            @RequestPart(value = "mongoclientFile") MultipartFile mongoclientFile,
            @RequestParam(value = "tempPath") String tempPath) throws Exception {
        request.getInputStream().skip(request.getContentLength());
        admDbService.installMongo(ids, tempPath, mongoFile, mongoclientFile, clientMountPath);
        return Result.success();
    }


    @ApiOperation(value = "redis安装", notes = "redis安装")
    @PostMapping(value = "installRedis", consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
    public Result installRedis(
            @RequestParam(value = "ids") String ids,
            @RequestParam(value = "tempPath") String tempPath,
            @RequestPart(value = "redisFile") MultipartFile redisFile) throws Exception {
        request.getInputStream().skip(request.getContentLength());
        admDbService.installRedis(ids, tempPath, redisFile);
        return Result.success();
    }

}
